package com.mobilekites.navigationmanager;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.support.v4.app.FragmentTransaction;

import com.mobilekites.R;
import com.mobilekites.activities.KitesActivity;
import com.mobilekites.util.MainThreadStack;

public class NavigationManager {

	private KitesActivity mActivity;
	private final Stack mBackStack = new MainThreadStack();
	private FragmentManager mFragmentManager;
	private List<OnNavigationTabChangeListener> mTabChangeListener = new ArrayList<NavigationManager.OnNavigationTabChangeListener>();
	private int mPlaceholder;

	public interface OnNavigationTabChangeListener {
		public void onNavigationTabChange(int placeholder);
	}

	public NavigationManager(KitesActivity mainactivity) {
		init(mainactivity);
	}

	public void addTabChangeListener(
			OnNavigationTabChangeListener mTabChangeListener) {
		this.mTabChangeListener.add(mTabChangeListener);
	}

	public void notifyTabChange(int placeholder) {
		for (OnNavigationTabChangeListener tabChangeListener : mTabChangeListener) {
			tabChangeListener.onNavigationTabChange(placeholder);
		}
	}

	public void init(KitesActivity mainactivity) {
		mActivity = mainactivity;
		mFragmentManager = mActivity.getSupportFragmentManager();
	}

	public void addOnBackStackChangedListener(
			OnBackStackChangedListener backStackChangedListener) {
		mFragmentManager
				.addOnBackStackChangedListener(backStackChangedListener);
	}

	public void removeOnBackStackChangedListener(
			OnBackStackChangedListener onbackstackchangedlistener) {
		mFragmentManager
				.removeOnBackStackChangedListener(onbackstackchangedlistener);
	}

	private boolean canNavigate() {
		return mActivity == null || mActivity.isStateSaved() ? false : true;
	}

	public boolean goBack() {
		if (mActivity == null || mActivity.isStateSaved()
				|| mBackStack.isEmpty())
			return false;
		NavigationState state = (NavigationState) mBackStack.peek();
		if (state.placeholder != mPlaceholder) {
			if (mTabChangeListener != null)
				notifyTabChange(state.placeholder);
			return true;
		}
		mBackStack.pop();
		mFragmentManager.popBackStack();
		return true;
	}

	public void setPlaceHolder(int placeHolder) {
		mPlaceholder = placeHolder;
	}

	@SuppressWarnings("unchecked")
	public void showPage(Fragment fragment) {
		if (!canNavigate())
			return;
		FragmentTransaction transaction = mFragmentManager.beginTransaction();
		transaction.setCustomAnimations(R.anim.fragment_enter,
				R.anim.fragment_exit, R.anim.fragment_pop_enter,
				R.anim.fragment_pop_exit);
		transaction.replace(mPlaceholder, fragment);
		NavigationState navigationState = new NavigationState(mPlaceholder);
		transaction.addToBackStack(navigationState.backStackName);
		mBackStack.push(navigationState);
		transaction.commit();
	}

	public void terminate() {
		mActivity = null;
	}

	public int getCurrentPlaceholder() {
		return mPlaceholder;
	}

	public int getCurrentPlaceholderInBackstack() {
		return !mBackStack.isEmpty() ? ((NavigationState) mBackStack.peek()).placeholder
				: 0;
	}

	public boolean isBackStackEmpty() {
		return mFragmentManager.getBackStackEntryCount() <= 0 ? true : false;
	}

	public Fragment getActivePage() {
		return mFragmentManager.findFragmentById(mPlaceholder);
	}

}
